<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="项目ID" prop="projectId">
        <el-input v-model="formData.projectId" placeholder="请输入项目ID" />
      </el-form-item>
      <el-form-item label="展项ID" prop="exhibitionId">
        <el-input v-model="formData.exhibitionId" placeholder="请输入展项ID" />
      </el-form-item>
      <el-form-item label="项目类别" prop="projectCategory">
        <el-select v-model="formData.projectCategory" placeholder="请选择项目类别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SH_DEMAND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目编号" prop="projectCode">
        <el-input v-model="formData.projectCode" placeholder="请输入项目编号" />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="formData.projectName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="启动时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          type="date"
          value-format="x"
          placeholder="选择启动时间"
        />
      </el-form-item>
      <el-form-item label="开工日期" prop="startWorkDate">
        <el-date-picker
          v-model="formData.startWorkDate"
          type="date"
          value-format="x"
          placeholder="选择开工日期"
        />
      </el-form-item>
      <el-form-item label="交付时间" prop="deliveryDate">
        <el-date-picker
          v-model="formData.deliveryDate"
          type="date"
          value-format="x"
          placeholder="选择交付时间"
        />
      </el-form-item>
      <el-form-item label="收款时间" prop="paymentDate">
        <el-date-picker
          v-model="formData.paymentDate"
          type="date"
          value-format="x"
          placeholder="选择收款时间"
        />
      </el-form-item>
      <el-form-item label="当前阶段" prop="currentStage">
        <el-input v-model="formData.currentStage" placeholder="请输入当前阶段" />
      </el-form-item>
      <el-form-item label="当前状态" prop="currentStatus">
        <el-radio-group v-model="formData.currentStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
      </el-form-item>
      <el-form-item label="数虎商务" prop="shangwuBiz">
        <el-input v-model="formData.shangwuBiz" placeholder="请输入数虎商务" />
      </el-form-item>
      <el-form-item label="数虎PM" prop="shangwuPm">
        <el-input v-model="formData.shangwuPm" placeholder="请输入数虎PM" />
      </el-form-item>
      <el-form-item label="客户PM" prop="customerPm">
        <el-input v-model="formData.customerPm" placeholder="请输入客户PM" />
      </el-form-item>
      <el-form-item label="客户采购" prop="customerPurchase">
        <el-input v-model="formData.customerPurchase" placeholder="请输入客户采购" />
      </el-form-item>
      <el-form-item label="需求预算" prop="demandBudget">
        <el-input v-model="formData.demandBudget" placeholder="请输入需求预算" />
      </el-form-item>
      <el-form-item label="PO号" prop="poNumber">
        <el-input v-model="formData.poNumber" placeholder="请输入PO号" />
      </el-form-item>
      <el-form-item label="开工令/PO/合同金额" prop="contractAmount">
        <el-input v-model="formData.contractAmount" placeholder="请输入开工令/PO/合同金额" />
      </el-form-item>
      <el-form-item label="物品开票-13%" prop="itemInvoicePercent">
        <el-input v-model="formData.itemInvoicePercent" placeholder="请输入物品开票-13%" />
      </el-form-item>
      <el-form-item label="服务开票-6%" prop="serviceInvoicePercent">
        <el-input v-model="formData.serviceInvoicePercent" placeholder="请输入服务开票-6%" />
      </el-form-item>
      <el-form-item label="已收金额" prop="receivedAmount">
        <el-input v-model="formData.receivedAmount" placeholder="请输入已收金额" />
      </el-form-item>
      <el-form-item label="未收金额" prop="outstandingAmount">
        <el-input v-model="formData.outstandingAmount" placeholder="请输入未收金额" />
      </el-form-item>
      <el-form-item label="商务流程进展" prop="bizProcessProgress">
        <el-input v-model="formData.bizProcessProgress" placeholder="请输入商务流程进展" />
      </el-form-item>
      <el-form-item label="项目执行进展" prop="executionProgress">
        <el-input v-model="formData.executionProgress" placeholder="请输入项目执行进展" />
      </el-form-item>
      <el-form-item label="交付里程碑" prop="deliveryMilestone">
        <el-input v-model="formData.deliveryMilestone" placeholder="请输入交付里程碑" />
      </el-form-item>
      <el-form-item label="项目交付件" prop="deliveryItems">
        <el-input v-model="formData.deliveryItems" placeholder="请输入项目交付件" />
      </el-form-item>
      <el-form-item label="项目交付地" prop="deliveryLocation">
        <el-input v-model="formData.deliveryLocation" placeholder="请输入项目交付地" />
      </el-form-item>
      <el-form-item label="项目备份" prop="backupInfo">
        <el-input v-model="formData.backupInfo" placeholder="请输入项目备份" />
      </el-form-item>
      <el-form-item label="合同额是否有新增" prop="additionalContract">
        <el-radio-group v-model="formData.additionalContract">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否有新增采购，未发起采购审批" prop="pendingPurchase">
        <el-radio-group v-model="formData.pendingPurchase">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="税前价" prop="purchaseTaxFreePrice">
        <el-input v-model="formData.purchaseTaxFreePrice" placeholder="请输入税前价" />
      </el-form-item>
      <el-form-item label="税率" prop="purchaseTaxRate">
        <el-input v-model="formData.purchaseTaxRate" placeholder="请输入税率" />
      </el-form-item>
      <el-form-item label="是否还有未报销费用（差旅等）" prop="unreimbursedExpenses">
        <el-radio-group v-model="formData.unreimbursedExpenses">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="未付尾款是否有无需支付的" prop="unpaidBalanceNoPaymentRequired">
        <el-radio-group v-model="formData.unpaidBalanceNoPaymentRequired">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="文件id" prop="fileIds">
        <el-input v-model="formData.fileIds" placeholder="请输入文件id" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { ProjectLedgerApi, ProjectLedgerVO } from '@/api/shuhu/projectledger'

/** 项目台账管理 表单 */
defineOptions({ name: 'ProjectLedgerForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  projectId: undefined,
  exhibitionId: undefined,
  projectCategory: undefined,
  projectCode: undefined,
  projectName: undefined,
  startTime: undefined,
  startWorkDate: undefined,
  deliveryDate: undefined,
  paymentDate: undefined,
  currentStage: undefined,
  currentStatus: undefined,
  customerName: undefined,
  shangwuBiz: undefined,
  shangwuPm: undefined,
  customerPm: undefined,
  customerPurchase: undefined,
  demandBudget: undefined,
  poNumber: undefined,
  contractAmount: undefined,
  itemInvoicePercent: undefined,
  serviceInvoicePercent: undefined,
  receivedAmount: undefined,
  outstandingAmount: undefined,
  bizProcessProgress: undefined,
  executionProgress: undefined,
  deliveryMilestone: undefined,
  deliveryItems: undefined,
  deliveryLocation: undefined,
  backupInfo: undefined,
  additionalContract: undefined,
  pendingPurchase: undefined,
  purchaseTaxFreePrice: undefined,
  purchaseTaxRate: undefined,
  unreimbursedExpenses: undefined,
  unpaidBalanceNoPaymentRequired: undefined,
  fileIds: undefined
})
const formRules = reactive({
  projectId: [{ required: true, message: '项目ID不能为空', trigger: 'blur' }],
  exhibitionId: [{ required: true, message: '展项ID不能为空', trigger: 'blur' }],
  projectCategory: [{ required: true, message: '项目类别不能为空', trigger: 'change' }],
  projectCode: [{ required: true, message: '项目编号不能为空', trigger: 'blur' }],
  projectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProjectLedgerApi.getProjectLedger(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProjectLedgerVO
    if (formType.value === 'create') {
      await ProjectLedgerApi.createProjectLedger(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProjectLedgerApi.updateProjectLedger(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    projectId: undefined,
    exhibitionId: undefined,
    projectCategory: undefined,
    projectCode: undefined,
    projectName: undefined,
    startTime: undefined,
    startWorkDate: undefined,
    deliveryDate: undefined,
    paymentDate: undefined,
    currentStage: undefined,
    currentStatus: undefined,
    customerName: undefined,
    shangwuBiz: undefined,
    shangwuPm: undefined,
    customerPm: undefined,
    customerPurchase: undefined,
    demandBudget: undefined,
    poNumber: undefined,
    contractAmount: undefined,
    itemInvoicePercent: undefined,
    serviceInvoicePercent: undefined,
    receivedAmount: undefined,
    outstandingAmount: undefined,
    bizProcessProgress: undefined,
    executionProgress: undefined,
    deliveryMilestone: undefined,
    deliveryItems: undefined,
    deliveryLocation: undefined,
    backupInfo: undefined,
    additionalContract: undefined,
    pendingPurchase: undefined,
    purchaseTaxFreePrice: undefined,
    purchaseTaxRate: undefined,
    unreimbursedExpenses: undefined,
    unpaidBalanceNoPaymentRequired: undefined,
    fileIds: undefined
  }
  formRef.value?.resetFields()
}
</script>